<?php
    chdir('../../');
    require('inc/header.php');
    define('NO_SESSION', 0);
    define("WRONG_USER", 1);
    define("WRONG_TASK", 2);
    define('ERROR_DELETING', 3);
    define('ERROR_ENDING', 4);
    define('ERROR_REMOVING',5);
    if(isset($_SESSION['user']['uid'])) {
        $uid = (int)$_SESSION['user']['uid'];
        $user = new AnyUser($uid);
        if($user->getUid() > 0) {
            switch($_GET['action']) {
                case 'removeUser':
                    $task = new ProjectTask((int)$_GET['ptid']);
                    if($task->getPtid() > 0) {
                        if($task->removeUser()) {
                            die(json_encode(array('error'=>-1,'ptid'=>$task->getPtid())));
                        } else {
                            $err = Error::instance();
                            die(json_encode(array('error'=>ERROR_DELETING,'info'=>$err->get())));
                        }
                    } else {
                        die(json_encode(array('error'=>WRONG_TASK)));
                    }
                break;
                case 'endTask':
                    $task = new ProjectTask((int)$_GET['ptid']);
                    if($task->getPtid() > 0) {
                        if($task->closeTask()) {
                            die(json_encode(array('error'=>-1,'ptid'=>$task->getPtid(),'dateDone'=>$task->getDateDone())));
                        } else {
                            $err = Error::instance();
                            die(json_encode(array('error'=>ERROR_ENDING,'info'=>$err->get())));
                        }
                    } else {
                        die(json_encode(array('error'=>WRONG_TASK)));
                    }
                break;
                case 'removeTask':
                    $task = new ProjectTask((int)$_GET['ptid']);
                    if($task->getPtid() > 0) {
                        if($task->deleteTask()) {
                            die(json_encode(array('error'=>-1,'ptid'=>$task->getPtid())));
                        } else {
                            $err = Error::instance();
                            die(json_encode(array('error'=>ERROR_REMOVING,'info'=>$err->get())));
                        }
                    } else {
                        die(json_encode(array('error'=>WRONG_TASK)));
                    }
                break;
            }
        } else {
            die(json_encode(array('error'=>WRONG_USER)));
        }
    } else {
        die(json_encode(array('error'=>NO_SESSION)));
    }
?>
